package controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.*;
import response.Result;
import service.ProductService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ProductController {
    @Resource
    private ProductService service;

    @RequestMapping("getProducts")
    public Page<Product> getProducts(int currentPage, int pageSize) {
        Page<Product> ProductPage = service.getAllByPage1(new Page<Product>(currentPage, pageSize, 0, null));
        return ProductPage;
    }

    @RequestMapping("getProducts1")
    public Result getProducts(Integer currentPage, Integer pageSize, HttpServletRequest request) {
        Page<Product> p = new Page<Product>();
        p.setCurrentPage(currentPage);
        p.setPageSize(pageSize);
        List<Product> productList = service.getAllByPage(p, new Product());
        p.setLists(productList);
        p.setTotalRecord(service.getCount(new Product()));
        return Result.ok(p);
    }

    @RequestMapping("getAdd")
    public int getAdd(@RequestBody Product product, HttpServletRequest request) {
        if (service.getProduct(product) == null) {
            int i = service.getProAdd(product);
            return i;
        } else {
            return 3;
        }
    }

    @RequestMapping("getUpd")
    public int getUpd(@RequestBody Product product) {
        System.out.println(product);
        int i = service.getProupd(product);
        return i;
    }

    @RequestMapping("delProduct")
    public int getProDel(String productCode) {
        int i = service.getProDel(productCode);
        return i;
    }

    @RequestMapping("queryStockCondition")
    public Result queryStockCondition(Integer currentPage, Integer pageSize,String productCode,String productName,int minNum,int maxNum, HttpServletRequest req) {
        Page<Product> page = new Page<Product>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        Product p = new Product();
        p.setProductCode(productCode);
        p.setName(productName);
        p.setMinNum(minNum);
        p.setMaxNum(maxNum);
        p.setAccount(scm.getAccount());
        int totalRecord = service.productCount(page,p);
        List<Product> list =service.productAllByPage(page,p);
        page.setLists(list);
        page.setTotalRecord(totalRecord);
        return Result.ok(page);
    }
    @RequestMapping("getProducts2")
    public Result getProducts2(Integer currentPage, Integer pageSize, HttpServletRequest req) {
        Page<Product> page = new Page<Product>();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);
        SCMUser scm =(SCMUser)req.getSession().getAttribute("loginUser");
        Product p = new Product();
        p.setAccount(scm.getAccount());
        int totalRecord = service.productCount1(page);
        List<Product> list =service.productAllByPage1(page);
        page.setLists(list);
        page.setTotalRecord(totalRecord);
        return Result.ok(page);
    }
    @RequestMapping("checkStock")
    public Result checkStock(@RequestBody Checkstock checkstock, HttpServletRequest req){
        SCMUser u=(SCMUser)req.getSession().getAttribute("loginUser");
        checkstock.setCreateUser(u.getName());
        service.addCheck(checkstock);
        return Result.ok(checkstock);
    }
    @RequestMapping("queryOutStockReport")
    public Result queryOutStockReport(Stockrecord stockrecord, String date, HttpServletRequest req) {
        Page<Stockrecord> page =new Page<Stockrecord>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        stockrecord.setAccount(u.getName());
        page.setCurrentPage(stockrecord.getCurrentPage());
        page.setPageSize(stockrecord.getPageSize());
        stockrecord.setDate(date);
        int totalrecord=service.outStockReportCount(page,stockrecord);
        List<Stockrecord> stockList = service.outStockReportPage(page,stockrecord);
        for (Stockrecord s :stockList){
            Product product = new Product();
            product.setProductCode(s.getProductCode());
            Product product1 = service.getProduct(product);
            s.setStockNum(-s.getStockNum());
            s.setSal((int) (product1.getPrice()*s.getStockNum()));
            s.setSoId(s.getOrderCode());
        }
        page.setTotalRecord(totalrecord);
        page.setLists(stockList);
        return Result.ok(page);
    }
    @RequestMapping("queryOutStockAllCount")
    public List<Stockrecord> queryOutStockAllCount(Stockrecord stockrecord,String date) {
        float sum = 0;
        int num = 0;
        stockrecord.setDate(date);
        List<Stockrecord> stockrecordList =service.outStockReportPage1(stockrecord);
        for (Stockrecord stock : stockrecordList) {
            num +=stock.getStockNum();
            sum +=stock.getPoTotal();
        }
        int totalrecord = service.outStockReportCount1(stockrecord);
        stockrecord.setStockPrcieOutToTal(sum);
        stockrecord.setStockIdOutCount(totalrecord);
        stockrecord.setStockNumOutCount(num);
        List<Stockrecord> list = new ArrayList<>();
        list.add(stockrecord);
        return list;
    }
    @RequestMapping("queryInStockReport")
    public Result queryInStockReport(Stockrecord stockrecord, String date, HttpServletRequest req) {
        Page<Stockrecord> page =new Page<Stockrecord>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        stockrecord.setAccount(u.getName());
        page.setCurrentPage(stockrecord.getCurrentPage());
        page.setPageSize(stockrecord.getPageSize());
        stockrecord.setDate(date);
        int totalrecord=service.inStockReportCount(page,stockrecord);
        List<Stockrecord> stockList1 = service.inStockReportPage(page,stockrecord);
        for (Stockrecord ss:stockList1){
            Product product = new Product();
            product.setProductCode(ss.getProductCode());
            Product product1 = service.getProduct(product);
            ss.setSal((int) (product1.getPrice()*ss.getStockNum()));
            ss.setPoId(ss.getOrderCode());
        }
        page.setTotalRecord(totalrecord);
        page.setLists(stockList1);
        return Result.ok(page);
    }
    @RequestMapping("queryInStockAllCount")
    public List<Stockrecord> queryInStockAllCount(Stockrecord stockrecord,String date) {
        float sum = 0;
        int num = 0;
        stockrecord.setDate(date);
        List<Stockrecord> stoList = service.inStockReportPage1(stockrecord);
        for (Stockrecord stock : stoList) {
            num +=stock.getStockNum();
            sum +=stock.getPoTotal();
        }
        int totalrecord = service.inStockReportCount1(stockrecord);
        stockrecord.setStockIdInCount(totalrecord);
        stockrecord.setStockPrcieInToTal(sum);
        stockrecord.setStockNumInCount(num);
        List<Stockrecord> list = new ArrayList<>();
        list.add(stockrecord);
        return list;
    }
    @RequestMapping("queryStockChangeReport")
    public Result queryStockChangeReport(Stockrecord stockrecord, String date, HttpServletRequest req) {
        Page<Stockrecord> page =new Page<Stockrecord>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        stockrecord.setAccount(u.getName());
        page.setCurrentPage(stockrecord.getCurrentPage());
        page.setPageSize(stockrecord.getPageSize());
        stockrecord.setDate(date);
        int totalrecord=service.stockChangeCount(page,stockrecord);
        List<Stockrecord> stockList2 = service.stockChangePage(page,stockrecord);
        page.setTotalRecord(totalrecord);
        page.setLists(stockList2);
        return Result.ok(page);
    }

    @RequestMapping("queryStockChangeAllCount")
    public List<Stockrecord> queryStockChangeAllCount(Stockrecord stockrecord,String date) {
        float sum = 0;
        int num = 0;
        stockrecord.setDate(date);
        List<Stockrecord> stoList = service.stockChangePage1(stockrecord);
        for (Stockrecord ssss : stoList) {
            num += ssss.getStockNum();

        }
        int totalrecord = service.stockChangeCount1(stockrecord);
        stockrecord.setProductCodeCount(totalrecord);
        stockrecord.setProductNum(num);
        List<Stockrecord> list = new ArrayList<>();
        list.add(stockrecord);
        return list;
    }
    @RequestMapping("getPayRecord")
    public Result getPayRecord(Payrecord payrecord, String date, HttpServletRequest req) {
        Page<Payrecord> page =new Page<Payrecord>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        payrecord.setAccount(u.getName());
        page.setCurrentPage(payrecord.getCurrentPage());
        page.setPageSize(payrecord.getPageSize());
        payrecord.setDate(date);
        int totalrecord=service.getPayRecordCount(page,payrecord);
        List<Payrecord> payList = service.getPayRecord(page,payrecord);
        page.setTotalRecord(totalrecord);
        page.setLists(payList);
        return Result.ok(page);
    }
    @RequestMapping("getPayRecord1")
    public Result getPayRecord1(Payrecord payrecord, String date, HttpServletRequest req) {
        Page<Payrecord> page =new Page<Payrecord>();
        SCMUser u = (SCMUser) req.getSession().getAttribute("loginUser");
        payrecord.setAccount(u.getName());
        page.setCurrentPage(payrecord.getCurrentPage());
        page.setPageSize(payrecord.getPageSize());
        payrecord.setDate(date);
        int totalrecord=service.getPayRecordCount1(page,payrecord);
        List<Payrecord> payList1 = service.getPayRecord1(page,payrecord);
        page.setTotalRecord(totalrecord);
        page.setLists(payList1);
        return Result.ok(page);
    }
    @RequestMapping("getPrice")
    public List<Payrecord> getPrice(String date) {
        float sum=0;
        float sprice=0;
        Payrecord pr = new Payrecord();
        pr.setDate(date);
        int num1 = service.getPayRecordInMoneyCount(pr);
        int num2 = service.getPayRecordOutMoneyCount(pr);
        List<Payrecord> payrecordList =service.getPayRecordOutMoney(pr);
        for (Payrecord payrecord : payrecordList) {
            sum+=payrecord.getPayPrice();
        }
        List<Payrecord> payrecordList1 =service.getPayRecordInMoney(pr);
        for (Payrecord payrecord : payrecordList1) {
            sprice+=payrecord.getPayPrice();
        }
        pr.setOutPrice(sum);
        pr.setInPrice(sprice);
        pr.setOutPriceCount(num2);
        pr.setInPriceCount(num1);
        List<Payrecord> list = new ArrayList<>();
        list.add(pr);
        return list;
    }
}

